function error_value = process_error_2d(mesh, pde, U, error_type)
psi = reference_basis_2d(mesh.basis_type);
dNdx = reference_basis_2d(mesh.basis_type, "dx");
dNdy = reference_basis_2d(mesh.basis_type, "dy");
switch error_type
    case "L2"
        sum_n = 0;
        for n = 1:mesh.Ne
            E = mesh.node(mesh.elem(n,:),:);
            V = E(1:3,:);
            J_det = abs((V(2,1)-V(1,1))*(V(3,2)-V(1,2)) - (V(2,2)-V(1,2))*(V(3,1)-V(1,1)));
            x_loc = @(x,y) ((x-V(1,1))*(V(3,2)-V(1,2)) - (y-V(1,2))*(V(3,1)-V(1,1)))/J_det;
            y_loc = @(x,y) ((V(2,1)-V(1,1))*(y-V(1,2)) - (V(2,2)-V(1,2))*(x-V(1,1)))/J_det;
            sum_u = @(x,y) 0;
            for i = 1:mesh.Nlb
                sum_u = @(x,y) sum_u(x,y) + U(mesh.elem(n,i)).*psi{i}(x_loc(x,y),y_loc(x,y));
            end
            f = @(x,y) (pde.u(x,y) - sum_u(x,y)).^2;
            r = gauss_int2d_tri(f, V);
            sum_n = sum_n + r;
        end
        error_value = sqrt(sum_n);

    case "H1"
        sum_nx = 0;
        sum_ny = 0;
        for n = 1:size(elem,1)
            E = node(elem(n,:),:);
            V = E(1:3,1:2);
            Jacobi = [V(2,:)-V(1,:); V(3,:)-V(1,:)]';
            J_det = det(Jacobi);
            J_inv = inv(Jacobi);
            uh_E = zeros(2,9);
            phi = cellfun(@(x,y) J_inv' * [x; y], phi_x, phi_y, "UniformOutput", false);
            for beta = 1:size(elem,2)
                uh_E = uh_E + U(elem(n,beta)) .* phi{beta};
            end
            uh_Ex = uh_E(1,:);
            uh_Ey = uh_E(2,:);
            ref_points = [ref_trans_tri{1}(:), ref_trans_tri{2}(:), ref_trans_tri{3}(:)]*V;
            f1 = (pde.dudx(ref_points(:,1),ref_points(:,2))' - uh_Ex).^2;
            f2 = (pde.dudy(ref_points(:,1),ref_points(:,2))' - uh_Ey).^2;
            rx = (gauss_weights_tri * f1') .* abs(J_det);
            ry = (gauss_weights_tri * f2') .* abs(J_det);
            sum_nx = sum_nx + rx;
            sum_ny = sum_ny + ry;
        end
        error_value = sqrt(sum_nx + sum_ny);
    otherwise
        error("Invalid error type.");
end
end